package com.bsfit.connectionpool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.zgkj.Zip.unzip;


/**
 * 连接池类
 *
 */
public class ConnectionSource {
	private static BasicDataSource dataSource = null;

	public ConnectionSource() {}

	public static void init() {
		if (dataSource != null) {
			try {
				dataSource.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			dataSource = null;
		}
		try {
			Properties prop = new Properties();
			String userpath = System.getProperty("user.dir");
			String propfile = userpath + System.getProperty("file.separator")+ "dbconfig.properties";
			String driver = "";
	        String url = "";
	        String user = "";
	        String password = "";

		      if ("0".equals(unzip.istest)) {
		        driver = "oracle.jdbc.driver.OracleDriver";
		        url = "jdbc:oracle:thin:@139.224.44.65:1521:xe";
		        user = "lf";
		        password = "Passw0rd_zgj";
		      } else if ("1".equals(unzip.istest)) {
		        driver = "oracle.jdbc.driver.OracleDriver";
		        url = "jdbc:oracle:thin:@139.224.44.65:1521:xe";
		        user = "mytest";
		        password = "Passw0rd_zgj_test";
		      } else if ("2".equals(unzip.istest)) {
		        driver = "oracle.jdbc.driver.OracleDriver";
		        url = "jdbc:oracle:thin:@localhost:1521:ORCL";
		        user = "lf";
		        password = "lf";
		      }
			String maxActive ="5";
			String maxIdle = "5";
			String maxWait = "10000";
			String removeAbandoned ="false";
			String removeAbandonedTimeout = "120";
			String testOnBorrow = "true";
			String logAbandoned = "true";


//			String driver = prop.getProperty("jdbc.driver");
//			String url = prop.getProperty("jdbc.url");
//			String user = prop.getProperty("jdbc.user");
//			String password = prop.getProperty("jdbc.password");
//			String maxActive = prop.getProperty("pool.maxactive");
//			String maxIdle = prop.getProperty("pool.maxidle");
//			String maxWait = prop.getProperty("pool.maxwait");
//			String removeAbandoned = prop.getProperty("pool.removeabandoned");
//			String removeAbandonedTimeout = prop.getProperty("pool.removeabandonedtimeout");
//			String testOnBorrow = prop.getProperty("pool.testonborrow");
//			String logAbandoned = prop.getProperty("pool.logabandoned");
//

			Properties p = new Properties();
			p.setProperty("driverClassName", driver);
			p.setProperty("url", url);
			p.setProperty("username", user);
			p.setProperty("password", password);
			p.setProperty("maxActive", maxActive);
			p.setProperty("maxIdle", maxIdle);
			p.setProperty("maxWait", maxWait);
			p.setProperty("removeAbandoned", removeAbandoned);
			p.setProperty("removeAbandonedTimeout", removeAbandonedTimeout);
			p.setProperty("testOnBorrow", testOnBorrow);
			p.setProperty("logAbandoned", logAbandoned);

			dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public static synchronized Connection getConnection() throws SQLException {
		if (dataSource == null) {
			init();
		}
		Connection conn = null;
		if (dataSource != null) {
			conn = dataSource.getConnection();
		}
		System.out.println("获取当前链接池使用个数：   "+dataSource.getNumActive());
		return conn;
	}

	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rset = null;
		try {
			System.out.println("Creating connection.");
			conn = ConnectionSource.getConnection();// 得到数据库的连接
			System.out.println("Creating statement.");
			stmt = conn.createStatement();// 创建数据库声明
			System.out.println("Executing statement.");
			rset = stmt.executeQuery("select * from msg_signed t order by txntype");// 从命令行得到要执行的数据库查询语句。
			System.out.println("Results:");
			int numcols = rset.getMetaData().getColumnCount();// 返回结果集的条数。
			while (rset.next()) {
				for (int i = 1; i <= numcols; i++) {
					System.out.print("\t" + rset.getString(i));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rset.close();
			} catch (Exception e) {
			}
			try {
				stmt.close();
			} catch (Exception e) {
			}
			try {
				conn.close();// 关闭数据库连接。
			} catch (Exception e) {
			}
		}
	}

}
